package com.ubao.weibo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import com.ubao.activity.MyApp;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.network.HttpHandler;
import com.ubao.network.ResponseInterface;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;


public class ShareTXWeiboActivity extends ShareWeiboActivity {


	public void onCreate(Bundle bundle) {
		
		super.onCreate(bundle);
	}
	

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onResume(){
		
		String temp = null;
		if(MyApp.getMyApp().weiboUser.isTxBind()){
			temp = MyApp.getMyApp().weiboUser.tx_name;
		}else{
			temp = "未绑定";
		}
		mTextBind.setText(temp);
		super.onResume();
	}

	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub

	}
	
	private void sendRequest(String content){
		String value =content;//;;"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";// mEditText.getText().toString().trim();
		
		//tx
		String data = WeiboConstant.sendWeiboDataTx(value);//"format=json&content="+URLEncoder.encode(value.trim())+"&clientip=0&jing=0&wei=0&syncflag=0";
		String url=WeiboConstant.sendWeiboUriTx(MyApp.getMyApp().weiboUser.tx_token,MyApp.getMyApp().weiboUser.tx_openid,"133.1.1.1");
		
		final SpecialRequest request = new SpecialRequest(url);
		request.postParams=data;
		
		request.msg_id = 1001;

		if (mHttpHandler == null) {
			mHttpHandler = new HttpHandler(this, 1000);
		}
		mHttpHandler.sendRequest(request);
	}
	
	@Override
	protected void httpCompleted(ResponseInterface resp) { 
		// TODO Auto-generated method stub
		
//		 {
		
			final SpecialResponse response = (SpecialResponse) resp;
			
			if(response.msg_id==1001){
				Log.e("content", "text:"+response.content);
				int resCode = mHttpHandler.getHttpClient().getResCode();
			//	if(resCode!=200){
					String  error= response.content;
					Log.e("error", error);
					
					int offset = error.indexOf("errcode")+9;
					int length = error.substring(offset).indexOf(",");
					int errorCode = Integer.parseInt(error.substring(offset,offset+length));
					Log.e("errorCode", ""+errorCode);
					
					handleError(errorCode);
			//	}else {
					//Log.e("success", ""+response.content);
				//}
			}
			
//				
				 
//				Message msg_new = new Message();
//				if (response.content != null
//						&& response.content.trim().equalsIgnoreCase("ok")) {
//					msg_new.arg1 = 1;
//				} else {
//					msg_new.arg1 = -1;
//				}
//			
//		}
	}
private void handleError(int errorCode){
	String str =null;
	if(errorCode==0){
		str="发送成功";
		Message msg =new Message();
		msg.obj=str;
		toastHandler.sendMessage(msg);
		GoBack.goBack(this);
		finish();
		
	}else
	if(errorCode==37){
		
		str="token已过期,请重新绑定";
		MyApp.getMyApp().weiboUser.removeBindTX();
	}else{
		str="错误"+errorCode;
	}
	
	Message msg =new Message();
	msg.obj=str;
	toastHandler.sendMessage(msg);
	}


//@Override
//public void onLeftButtonClick() {
//	// TODO Auto-generated method stub
//	GoBack.goBack(this);
//	
//}
//@Override
//public void onRightButtonClick() {
//	// TODO Auto-generated method stub
//	if(MyApp.getMyApp().weiboUser.isTxBind()){
//		sendRequest(getSendContent());
//	}else{
//		setGoBack();
//		Intent intent = new Intent(this,
//				TXWeiboOAuthActivity.class);
//		startActivity(intent);
//	}
//}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if(keyCode==KeyEvent.KEYCODE_BACK){
		GoBack.goBack(this);
		return true;
	}
	// TODO Auto-generated method stub
	return super.onKeyDown(keyCode, event);
}


}
